import os
from google.appengine.ext.webapp import template
import cgi
from owner import Owner
from product import Product
from wish import Wish

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class MainPage(webapp.RequestHandler):
	def get(self):
		owners = Owner.all().count()
		wishes = Wish.all().count()
		products = Product.all().count()
		if users.get_current_user():
		   url = users.create_logout_url(self.request.uri)
		   url_linktext = 'Logout'  
		   template_values = {
			  'url': url,
			  'url_linktext': url_linktext,
			  'owners': owners,
			  'wishes': wishes,
			  'email': users.get_current_user().email(),
		  'products': products,
			  }
		else:
			template_values = {
				'owners': owners,
				'wishes': wishes,
				'products': products
			  }

		path = os.path.join(os.path.dirname(__file__), 'index.html')
		self.response.out.write(template.render(path, template_values))
    
	def getCurrentOwner(self):
		loggedInuser = users.get_current_user()

		if loggedInuser:
			try:
				owner = Owner.find(loggedInuser.email())
			except NotFound:
				owner = null
		else:
			owner = null
		return owner
	
    
application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
